package cn.lch.structure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;

/**
 * @author lch
 * @version 1.0
 * @date 2024/7/1
 * @description: TODO
 */
public class Graph {
    //节点列表
    public HashMap<Integer, Node> nodes;
    //边界列表
    public HashSet<Edge> edges;
    //节点个数
    public int nodeNum;
    //边个数
    public int edgeNum;


    public Graph() {
        nodes = new HashMap<>();
        edges = new HashSet<>();
        nodeNum = 0;
        edgeNum = 0;
    }

    public class Node {
        public int value;//值
        public int in;//入度
        public int out;//出度
        public ArrayList<Node> nexts;//从自己连出去的下一个节点列表
        public ArrayList<Edge> edges;//从自己连出去的边

        public Node(int value) {
            this.value = value;
            in = 0;
            out = 0;
            nexts = new ArrayList<>();
            edges = new ArrayList<>();
        }
    }

    public class Edge {
        public int weight;//权重
        public Node from;//来源
        public Node to;//目的地

        public Edge(int weight, Node from, Node to) {
            this.weight = weight;
            this.from = from;
            this.to = to;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            Edge edge = (Edge) o;
            return weight == edge.weight && Objects.equals(from, edge.from) && Objects.equals(to, edge.to);
        }

        @Override
        public int hashCode() {
            return Objects.hash(weight, from, to);
        }
    }
}
